﻿
namespace FastFood.WindowsService
{
    using System;
    using System.Configuration;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Timers;
    using FastFood.Business.Entities;
    using FastFood.Business.Entities.Tipos;
    using FastFood.Business.Logic;

    public partial class FastFoodService : ServiceBase
    {
        #region Atributos

        Timer timer = new Timer();

        #endregion

        #region Propiedades

        public int Minutos
        {
            get { return Convert.ToInt32(ConfigurationManager.AppSettings.Get("Minutos")); }
        }

        #endregion

        #region Constructor

        public FastFoodService()
        {
            InitializeComponent();
        }

        #endregion

        #region Eventos

        protected override void OnStart(string[] args)
        {
            if (args.GetLength(0) > 0 && args[0].Equals("DEBUG")) Debugger.Launch();

            timer.Interval = Minutos;
            timer.Elapsed += new ElapsedEventHandler(WorkerFunction);
            timer.Enabled = true;
        }

        protected override void OnStop()
        {
            timer.Enabled = false;
        }

        #endregion

        #region Metodos

        private void WorkerFunction(object sender, ElapsedEventArgs e)
        {
            var fecha = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

            try
            {
                var insumos = InsumosManager.Instancia.Listar();

                foreach (var insumo in insumos)
                {
                    if (insumo.stock != 0) continue;

                    var log = LogServicioManager.Instancia.ListarPorIdInsumo(insumo.insumoID, fecha);
                    if (log.Count == 0)
                    {
                        LogServicioManager.Instancia.ModificarPorIdInsumo(insumo.insumoID, (int)TipoEstadoServicio.Revisado);
                        LogServicioManager.Instancia.Agregar(
                            new LogServicio
                            {
                                FechaAlerta = fecha,
                                Descripcion = insumo.nombre + " SIN STOCK.",
                                IdTipo = (int)TipoAlerta.SinStock,
                                IdEstado = (int)TipoEstadoServicio.Nuevo,
                                IdInsumo = insumo.insumoID,
                                FechaCreacion = DateTime.Now
                            });
                    }
                }

                var mensaje = string.Empty;
                var alertas = LogServicioManager.Instancia.ListarBySearch(null, fecha, null, (int)TipoEstadoServicio.Nuevo);

                foreach (var alerta in alertas)
                {
                    mensaje += alerta.Descripcion + Environment.NewLine;
                }

                if (!string.IsNullOrEmpty(mensaje))
                {
                    var frm = new FrmAlerta(mensaje);
                    frm.Show();
                }
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }

        #endregion
    }
}
